<template>
	<view>
		<view class="body">
			<view class="kong"></view>
			<view class="imgs">
				<image src="../../../static/image/iconp2.png" mode="aspectFit" class="imagus"></image>
			</view>
			<!-- 要做循环的开始-->
			<block v-if="isShow">
				<block v-for="(item,index) in list" :key="index">
					<view class="iBody dis dis2 dis1">
						<image :src="item.imgUrl" mode="aspectFit" class="fash"></image>
						<text class="iText">{{item.title}}</text>
					</view>
					<view class="items">
						<block v-for="(i,index1) in item.listGood" :key="index1">
							<view class="itemTop_1 dis">
								<view class="itemTop dis dis2">
									<image src="../../../static/image/iconp4_1.png" mode="aspectFit" class="images"></image>
									<text class="itemText1">{{i.title}}</text>
								</view>
								<view class="item_1 dis dis6">
									<block v-for="(t,index3) in i.pj" :key="index3">
										<block v-if="t.data">
											<view class="item contenr" v-for="(s,index4) in t.data" :key="index4" @click="dels(s.id,index,index1,index3,index4)">
												<text>{{s.title}}</text>
												<image src="../../../static/img/close.png" mode="aspectFit" class="close"></image>
											</view>
										</block>
										<view class="item contenr" v-else @click="dels(t.id,index,index1,index3,-1)">
											<text>{{t.title}}</text>
											<image src="../../../static/img/close.png" mode="aspectFit" class="close"></image>
										</view>
									</block>
								</view>
							</view>
							
						</block>
					</view>
				</block>
			</block>
			<!-- 循环结束 -->
			<block v-else>
				<block v-if="is">
					<view class="coolskong dis dis5 dis1 dis2">
						<image src="../../../static/img/zhuyiico.png" mode="aspectFit" class="zhuyi"></image>
						<text class="zhuyiText">本次体检暂无更新喔~</text>
					</view>
				</block>
			</block>
			<view class="kongsTop">
				
			</view>
		</view>
		<view class="kongs"></view>
		<view class="btn contenr">
			<view class="btons contenr" @click="subitm" v-if="list.length>0">
				生成报告
			</view>
			<view class="btons btons_1 contenr" v-else>
				确认生成
			</view>
		</view>
	</view>
</template>

<script>
	let app = getApp().globalData;
	import getData from '@/common/js/indexFx.js';
	export default {
		data() {
			return {
				list: [],
				is: false,
				isShow:true,
				daily:1,/*0正常，1是日常体检*/
			};
		},
		methods: {
			subitm() {
				let that = this;
				this.$http._post('examination/generatereport.do').then(res => {
					console.log(res)
					if (res.data.issuccess == 1) {
					 that.getResult()
						app.user.isexamiate=1;
						// if(is){
							// setTimeout(() => {
							
							// }, 2000)
						// }
						
					} else {
						that.$http.isMssage(res.data.msg)
					}
				})
			},
			getData() {
				let that = this;
				let list = []
				this.$http._post('examination/confirm.do').then(res => {
					console.log(res)
					if (res.data.issuccess == 1) {
						let {
							data
						} = res.data;
						let clas = data.class;
						// let select = data.select;
						let select = that.getData_1(data.select);
						let list = []
						for (let i = 0; i < clas.length; i++) {
							let data = []
							for (let j = 0; j < select.length; j++) {
								// 将所有用户选中的分组合并到一个数组
								if (clas[i].classid == select[j].classid) {
									data.push(select[j]);
								}

							}
							if (data.length > 0) {
								list.push({
									data: data,
									title: clas[i].title,
									classid: clas[i].classid,
									parentid: clas[i].parentid
								})
							}

						}
						console.log('list', list)
						this.classify(list, clas, select)

					}
				});
			},
			// 选中去重
			getData_1(select) {
				for (let i = 0; i < select.length; i++) {
					for (let j = i + 1; j < select.length; j++) {
						if (select[i].title == select[j].title) {
							select.splice(j, 1);
							j--;
						}
					}
				}
				return select
			},
			classify(list, clas, select) {
				let good = [];
				// 将数据分组，分类分成第一类
				for (let i = 0; i < clas.length; i++) {
					if (clas[i].parentid == 0) {
						good.push(clas[i])
					}
				}
				// 将数据分成第二类并拼接
				for (let j = 0; j < good.length; j++) {
					let listGood = []
					for (let p = 0; p < clas.length; p++) {
						if (good[j].classid == clas[p].parentid) {
							listGood.push(clas[p])
						}
					}
					// console.log('listGood',listGood)
					good[j].listGood = listGood
				}
				// console.log("goods1", good)
				this.addArr(good, list)
			},
			addArr(good, list) {
				// 合并数组
				for (let j = 0; j < good.length; j++) {
					let aj = []
					for (let s = 0; s < good[j].listGood.length; s++) {
						good[j].listGood[s].pj = [];
					}
					for (let i = 0; i < list.length; i++) {
						//如果选择的数组中的parentid和所有数据数组中的calssid相等的话
						if (good[j].classid == list[i].parentid) {
							// console.log('list[i]',list[i])
							list[i].pj=list[i].data
							good[j].listGood.push(list[i])
						} else {
							for (let p = 0; p < good[j].listGood.length; p++) {
								
								//如果选择的数组中的parentid和所有数据数组中的第二层数组中的calssid相等的话
								if (list[i].parentid == good[j].listGood[p].classid) {
									
									good[j].listGood[p].pj.push(list[i])
									// console.log('list[i]222',good[j].listGood[p].pj)
								}

							}
						}
					}
					if (aj.length > 0) {
						console.log('aj', aj)
						// good[j].listGood.push(aj)
					}

				}
				console.log('good', good)
				this.delArr(good)
			},
			delArr(good) {
				// 整理数组
				let that = this;
				for (let i = 0; i < good.length; i++) {
					for (let j = 0; j < good[i].listGood.length; j++) {
						let lists = good[i].listGood[j].title.split(',')
						good[i].listGood[j].title = lists[0]
						if (good[i].listGood[j].data) {
							good[i].listGood[j].pj = good[i].listGood[j].data
						}
						if (good[i].listGood[j].pj.length == 0) {
							good[i].listGood.splice(j, 1)
							j = j - 1
						}
					}
					if (good[i].listGood.length == 0) {
						good.splice(i, 1);
						i = i - 1
					}

				}
				for (let s = 0; s < good.length; s++) {
					let arr = good[s].title.split(',');
					good[s].title = arr[0];
					good[s].imgUrl = arr[1]
				}
				that.list = good;
				if (that.list.length <= 0) {
					that.isShow = false;
					that.is = true;
				}
				console.log('good1', good)
			},
			getResult() {
				let that = this;
				let is=false;
				getData.everyday().then(res=>{
					if(res.data.issuccess==1){
						let {data} = res.data;
						app.progresstext=data.age
						app.progresstext1=data.score_zhf
						app.retuData = data;
						if(data.age==-1){
							app.progresstext='?'
						}
						if(data.score_zhf==-1){
							app.progresstext1='?'
						}
						let list = data.weidulist;
						let listpo = [];
						let values = [];
						for (let i = 0; i < list.length; i++) {
							let num = (list[i].score).toFixed(0);
							let name = `${list[i].weidu}\n${num}`;
						
							if (list[i].weidu == '上火' && list[i].isempty == 1) {
								name = `${list[i].weidu}\n？`;
							}
							listpo.push({
								name,
								max: 100
							});
							values.push(list[i].score)
						}
						app.listRadar1 = listpo;
						app.value1 = values;
						is= true;
						uni.reLaunch({
							url: `/second/me/everydayResult/everydayResult`
						})
					}else{
						is=false	
					}
					console.log(res)
				
				})
				
				// this.$http._post('examination/showreport.do', {
				// 	id: 0
				// }).then(res => {
				// 	if (res.data.issuccess == 1) {
				// 		let {
				// 			data
				// 		} = res.data;
				// 		app.ensue = data;
				// 		app.user.isexamiate=1;
				// 		if(data.zhibiao=='正常' || data.zhibiao==''){
				// 			that.daily=0
				// 		}else{
				// 			that.daily=1
				// 		}
				// 		that.$http.isMssage('体检报告生成成功')
				// 	} else {
				// 		that.$http.isMssage(res.data.msg)
				// 	}
				// 	console.log(res)
				// })
			},
			dels(id, index, index1, index3, index4) {
				let list = this.list;
				let that = this;
				this.$http._post('examination/removeuseritem.do', {
					id: id
				}).then(res => {
					if (res.data.issuccess == 1) {
						if (index4 >= 0) {
							list[index].listGood[index1].pj[index3].data.splice(index4, 1);
							that.remove(list)
						} else {
							list[index].listGood[index1].pj.splice(index3, 1);
							that.remove(list)
						}
						
					} else {
						return
					}

				})
			},
			remove(list) {
				let that =this;
				for (let i = 0; i < list.length; i++) {
					for (let j = 0; j < list[i].listGood.length; j++) {
						for (let y = 0; y < list[i].listGood[j].pj.length; y++) {
							if (list[i].listGood[j].pj[y].data) {
								if (list[i].listGood[j].pj[y].data.length == 0) {
									list[i].listGood[j].pj.splice(y, 1);
									y = y - 1;
								}
							}
						}
						if (list[i].listGood[j].pj.length == 0) {
							list[i].listGood.splice(j, 1);
							j = j - 1;
						}
					}
					if (list[i].listGood.length == 0) {
						list.splice(i, 1);
						i = i - 1;
					}
				}
				console.log('list', list)
				if(list.length==0){
					that.isShow = false;
					that.is = true
				}
				this.list = list;
				this.$forceUpdate();
			}
		},
		onLoad() {
			
			this.getData()
		},

	}
</script>

<style lang="scss">
	@import "../../../common/css/flex.css";
	@import "../../../common/css/index.scss";

	.body {
		width: 686upx;
		min-height: 398upx;
		background: #FFFFFF;
		border-radius: 24upx;
		margin: auto;
		margin-top: 48upx;
		overflow: hidden;

		.kong {
			width: 100%;
			height: 40upx;
			// border: 1px solid red;
		}

		.imgs {
			width: 100%;
			height: 56upx;

			.imagus {
				width: 240upx;
				height: 56upx;
			}

		}

		.iBody {
			width: 100%;
			// height: 168upx;
			// 
			// border: 1px solid red;
			margin-top: 60upx;

			.fash {
				width: 128upx;
				height: 128upx;
				margin-right: 16upx;
			}

			.iText {
				font-size: 32upx;
				font-family: $typeface;
				font-weight: 500;
				color: #333333;
			}
		}

		.items {
			background: #FFFFFF;
			box-shadow: 0px 8upx 12upx 0px rgba(0, 0, 0, 0.06);
			border-radius: 16upx;
			// width: 100%;
			width: 622upx;
			margin: auto;
			margin-top: 14upx;
			min-height: 105upx;

			.itemTop_1 {
				margin-bottom: 14upx;
				margin-top: 14upx;

			}
			.itemTop {
				height: 70upx;
				margin-right: 8upx;
				
				.images {
					width: 12upx;
					height: 24upx;
				}

				.itemText1 {
					font-size: 28upx;
					font-family: $typeface;
					font-weight: 500;
					color: #333333;
					margin-left: 16upx;
					// padding:25upx;
					white-space:nowrap; 
				}
			}

			.item_1 {
				// width: 100%;
				min-height: 100upx;
				margin-right: 30upx;
				margin-bottom: 16upx;

				.item {
					height: 48upx;
					background: #F8F8F8;
					border-radius: 28upx;
					font-size: 24upx;
					font-family: $typeface1;
					font-weight: 400;
					color: #666666;
					padding: 2upx 24upx;
					margin: 10upx 0 12upx 28upx;
					position: relative;
					
					.close {
						width: 26upx;
						height: 26upx;
						position: absolute;
						top: -10upx;
						right: -10upx;
					}
				}
			}
		}

		

		.coolskong {
			width: 100%;
			height: 300upx;

			.zhuyi {
				width: 58upx;
				height: 58upx;
				margin-bottom: 15upx;
			}

			.zhuyiText {
				font-size: 30upx;
				font-family: $typeface1;
				font-weight: 400;
				color: #999999;

			}
		}
		.kongsTop{
			width: 100%;
			height: 60upx;
		}

	}

	.btn {
		width: 750upx;
		height: 128upx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		border-top: 0.5px solid #e8e8e8;
		.btons {
			width: 446upx;
			height: 88upx;
			background: #FFD344;
			border-radius: 44upx;
			font-size: 32upx;
			font-family: $typeface;
			font-weight: 500;
			color: #333;
		}

		.btons_1 {
			background-color: #c1c1c1;
		}
	}
	.kongs {
		width: 100%;
		height: 200upx;
	}
</style>
